package ecstore.client;

import ecstore.client.customWidgets.*;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import com.google.gwt.user.client.ui.DecoratedStackPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarSeparator;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Cstore implements EntryPoint {

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final LoginServiceAsync loginService = GWT.create(LoginService.class);

	private void loadLoginUI(LoginInfo loginInfo) {
		System.out.println("loginning...");
		VerticalPanel loginPanel = new VerticalPanel();
		Anchor loginLink = new Anchor("Sign In");
		loginLink.setHref(loginInfo.getLoginUrl());
//		loginPanel.add(logo);
		loginPanel.add(new Label("Please sign-in with your Google Account to access the Time Entry application."));
		loginPanel.add(loginLink);
		RootPanel.get("logoutContainer2").add(loginPanel);
		}
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		final Anchor loginAnchor = new Anchor("Sign in  ", "login", "_blank");
		final Anchor logoutAnchor = new Anchor("Sign out", "logout", "_blank");
		
		final RootPanel mainPanel = RootPanel.get("mainPage");
		final RootPanel sidebarPanel = RootPanel.get("sideBar");
		
		mainPanel.setWidth("100%");
		mainPanel.setHeight(Window.getClientWidth()+"px");
		
		DecoratorPanel shopNamePanel = new DecoratorPanel();
		mainPanel.add(shopNamePanel, 5, 5);
		shopNamePanel.setSize("439px", "57px");
		
		Label lblEppingConvenienceStore = new Label("Epping Convenience Store\nManagement System");
		shopNamePanel.setWidget(lblEppingConvenienceStore);
		
		
		DecoratedStackPanel stackPanel = new DecoratedStackPanel();
		sidebarPanel.add(stackPanel, 0, 98);
		stackPanel.setSize("195px", "358px");
		
		// The content panel which used by other widgets
		final DecoratorPanel contentPanel = new DecoratorPanel();
		mainPanel.add(contentPanel, 196, 98);
		contentPanel.setSize("439px", "358px");
		
		Tree tree = new Tree();
		stackPanel.add(tree, "Shop View", false);
		tree.setSize("100%", "100%");
		
		Label menuTobaccoLabel = new Label("Tobacco");

		tree.addItem(menuTobaccoLabel);
				
		DecoratedStackPanel orderManagerSideBar_1 = new DecoratedStackPanel();
		stackPanel.add(orderManagerSideBar_1, "Order Manager", false);
		orderManagerSideBar_1.setSize("100%", "100%");
		
		Tree itemManager = new Tree();
		stackPanel.add(itemManager, "Item Management", false);
		itemManager.setSize("100%", "100%");
		
		// Show the panel to add a new item
		Label addItemLabel = new Label("Add an item");
		final AddItemPanel additemPanel = new AddItemPanel();
		addItemLabel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				contentPanel.clear();
				contentPanel.setWidget(additemPanel);
			}
		});
		
		itemManager.addItem(addItemLabel);
		
		
		final Label searchItemLabel = new Label("Find an item");
		final SearchItemPanel searchPanel = new SearchItemPanel();
		searchItemLabel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				contentPanel.clear();
				contentPanel.setWidget(searchPanel);
			}
		});
		itemManager.addItem(searchItemLabel);
		

		
		final CigarettesPanel cigarettesPanel = new CigarettesPanel();
		contentPanel.setWidget(cigarettesPanel);
		


		final Label loggedUser = new Label("loginUser");


		
		menuTobaccoLabel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				contentPanel.clear();
				contentPanel.setWidget(cigarettesPanel);
			}
		});
		
		LoginInfo loginInfo = new LoginInfo();
		/*
		 * Show login Status labels on the upper-right corner 
		 */
		loginService.login(GWT.getHostPageBaseURL(), new AsyncCallback<LoginInfo>() {
			public void onSuccess(LoginInfo result){
				LoginInfo loginInfo = result;
				
				if(loginInfo.isLoggedIn()){
					loginInfo.setLoggedIn(true);
					loggedUser.setText(loginInfo.getEmailAddress());
					RootPanel.get("loginContainer").add(loggedUser);
					logoutAnchor.setHref(loginInfo.getLogoutUrl());
					RootPanel.get("logoutContainer").add(logoutAnchor);
				} else {
					loginInfo.setLoggedIn(false);
					loginAnchor.setHref(loginInfo.getLoginUrl());
					RootPanel.get("loginContainer").add(loginAnchor);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				System.out.println("login failed");
			}

		});
		
		
		/*
		 * Login management interface if user is logged.
		 */
		loginInfo.setLoggedIn(true);
		if(loginInfo.isLoggedIn()){
			
		} else {

		}
		
		/*
		 * Logout handler 
		 */
		logoutAnchor.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event){
				
			}
			
		});
		
		Window.addResizeHandler(new ResizeHandler() {
			public void onResize(ResizeEvent event) {
				int height = event.getHeight();
				mainPanel.setHeight(height + "px");
			}
		});

	}
}
